//
//  Bx_AfnNetManager.m
//  BiXuan
//
//  Created by ayi on 2018/7/24.
//  Copyright © 2018年 BSNL. All rights reserved.
//

#import "ZY_AfnNetManager.h"

#import <MJExtension/MJExtension.h>
#import <YYCategories/NSString+YYAdd.h>
#import <YYCategories/NSDate+YYAdd.h>
#import "BaseNetModel.h"
#import "PPNetworkCache.h"
#import <sys/utsname.h>

@interface ZY_AfnNetManager()



@end

@implementation ZY_AfnNetManager

static NSMutableArray *_tasks;

+ (ZY_AfnNetManager *)shareInstance{
    static ZY_AfnNetManager *afnmanager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        afnmanager = [[ZY_AfnNetManager alloc]init];
        afnmanager.manager = [self defaultSessionManager];
    });
    return afnmanager;
}

//所有tasks
+ (NSMutableArray *)tasks {
    if (!_tasks) {
        _tasks = [[NSMutableArray alloc] init];
    }
    return _tasks;
}
/**
 取消所有的请求
 */
+ (void)cancelAllRequest{
    @synchronized(self) {
        [[self tasks] enumerateObjectsUsingBlock:^(NSURLSessionTask  *_Nonnull task, NSUInteger idx, BOOL * _Nonnull stop) {
            [task cancel];
        }];
        [[self tasks] removeAllObjects];
    }
}
/**
 根据url取消请求
 */
+ (void)cancelAppointRequest:(NSString *)url{
    if (!url) { return; }
    @synchronized (self) {
        [[self tasks] enumerateObjectsUsingBlock:^(NSURLSessionTask  *_Nonnull task, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([task.currentRequest.URL.absoluteString hasSuffix:url]) {
                [task cancel];
                [[self tasks] removeObject:task];
                *stop = YES;
            }
        }];
    }
}
/**
 根据task取消请求
 */
+ (void)cancelAppointTaskt:(NSURLSessionTask *)task{
    if (!task) { return; }
    @synchronized (self) {
        [[self tasks] enumerateObjectsUsingBlock:^(NSURLSessionTask  *_Nonnull onetask, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([onetask.currentRequest.URL.absoluteString hasPrefix:task.currentRequest.URL.absoluteString]) {
                [onetask cancel];
                [[self tasks] removeObject:onetask];
                *stop = YES;
            }
        }];
    }
}


+ (AFHTTPSessionManager *)defaultSessionManager{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.requestSerializer.timeoutInterval = 15;
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalAndRemoteCacheData;
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithArray:@[
                                                                              @"application/json",
                                                                              @"text/html",
                                                                              @"text/json",
                                                                              @"text/plain",
                                                                              @"text/javascript",
                                                                              @"text/xml",
                                                                              @"text/text"
//                                                                              @"sparql-results+json"
                                                                              ]];
    return manager;
}


+ (void)startMonitoring
{
    // 1.获得网络监控的管理者
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    // 2.设置网络状态改变后的处理
    [mgr setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        // 当网络状态改变了, 就会调用这个block
        switch (status)
        {
            case AFNetworkReachabilityStatusUnknown: // 未知网络
            {
                NSLog(@"未知网络");
                [ZY_AfnNetManager shareInstance].networkStats = StatusUnknown;
                break;
            }
            case AFNetworkReachabilityStatusNotReachable: // 没有网络(断网)
            {
                NSLog(@"没有网络");
                [ZY_AfnNetManager shareInstance].networkStats = StatusNoReachable;
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWWAN: // 手机自带网络
            {
                NSLog(@"手机自带网络");
                [ZY_AfnNetManager shareInstance].networkStats = StatusReachableViaWAN;
                break;
            }
            case AFNetworkReachabilityStatusReachableViaWiFi: // WIFI
            {
                [ZY_AfnNetManager shareInstance].networkStats = StatusReachableViaWiFi;
                NSLog(@"WIFI--%lu",(unsigned long)[ZY_AfnNetManager shareInstance].networkStats);
                break;
            }
        }
    }];
    [mgr startMonitoring];
}


/**
  装填url

 @param url url
 @return 返回新的url
 */
- (NSString *)baseUrlAppend:(NSString *)url{
    return [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
//    return [[NSString stringWithFormat:@"%@%@",BASE_URL,url] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
}

/**
 签名操作
 
 @param param 传入的数据
 @return 返回签好名的字典
 */
- (NSDictionary *)signPackageWithParam:(NSDictionary *)param andMd5Name:(NSString *)md5name{
    //判断是否存在了签名
    if (![param objectForKey:@"FKEY"]) {
        NSMutableDictionary *paramNew = [NSMutableDictionary dictionaryWithDictionary:param];
        
        NSString *fkey = [NSString stringWithFormat:@"%@%@%@",md5name?:@"NULL",[[NSDate date]  stringWithFormat:@"yyyyMMdd"],@",car,"];
        [paramNew setObject:[fkey md5String] forKey:@"FKEY"];

        return paramNew;
    }
    return param;
}





- (NSURLSessionTask *)getWithURL:(NSString *)url
                          params:(NSDictionary *)params
                     md5Paraname:(nullable NSString *)md5name
                         success:(ResponseSuccess )success
                            fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Get" andWithURL:url params:params md5Paraname:md5name request:nil response:nil responseCache:nil success:success netError:fail];
}

- (NSURLSessionTask *)getWithURL:(NSString *)url
                          params:(NSDictionary *)params
                     md5Paraname:(nullable NSString *)md5name
                   responseCache:(ResponseCache )cachecallback
                         success:(ResponseSuccess )success
                            fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Get" andWithURL:url params:params md5Paraname:md5name request:nil response:nil responseCache:cachecallback success:success netError:fail];
}

- (NSURLSessionTask *)postWithURL:(NSString *)url
                           params:(id)params
                      md5Paraname:(nullable NSString *)md5name
                          success:(ResponseSuccess)success
                             fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Post" andWithURL:url params:params md5Paraname:md5name request:nil response:nil responseCache:nil success:success netError:fail];
}

- (NSURLSessionTask *)postWithURL:(NSString *)url
                           params:(id)params
                      md5Paraname:(nullable NSString *)md5name
                    responseCache:(ResponseCache )cachecallback
                          success:(ResponseSuccess)success
                             fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Post" andWithURL:url params:params md5Paraname:md5name request:nil response:nil responseCache:cachecallback success:success netError:fail];
}






//1
- (NSURLSessionTask *)getWithURL:(NSString *)url
                          params:(NSDictionary *)params
                     md5Paraname:(nullable NSString *)md5name
                         request:(nullable BaseNetRequestModel *)requestModel
                        response:(nullable BaseNetResponseModel *)responseModel
                         success:(ResponseSuccess )success
                            fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Get" andWithURL:url params:params md5Paraname:md5name request:requestModel response:responseModel responseCache:nil success:success netError:fail];
}
//2
- (NSURLSessionTask *)getWithURL:(NSString *)url
                          params:(NSDictionary *)params
                     md5Paraname:(nullable NSString *)md5name
                         request:(nullable BaseNetRequestModel *)requestModel
                        response:(nullable BaseNetResponseModel *)responseModel
                   responseCache:(ResponseCache )cachecallback
                         success:(ResponseSuccess )success
                            fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Get" andWithURL:url params:params md5Paraname:md5name request:requestModel response:responseModel responseCache:cachecallback success:success netError:fail];
}

//3
- (NSURLSessionTask *)postWithURL:(NSString *)url
                           params:(id)params
                      md5Paraname:(nullable NSString *)md5name
                          request:(nullable BaseNetRequestModel *)requestModel
                         response:(nullable BaseNetResponseModel *)responseModel
                          success:(ResponseSuccess)success
                             fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Post" andWithURL:url params:params md5Paraname:md5name request:requestModel response:responseModel responseCache:nil success:success netError:fail];
}

//4
- (NSURLSessionTask *)postWithURL:(NSString *)url
                           params:(id)params
                      md5Paraname:(nullable NSString *)md5name
                          request:(nullable BaseNetRequestModel *)requestModel
                         response:(nullable BaseNetResponseModel *)responseModel
                    responseCache:(ResponseCache )cachecallback
                          success:(ResponseSuccess)success
                             fail:(ResponseFail )fail{
    return [self afn_DataIsGet:@"Post" andWithURL:url params:params md5Paraname:md5name request:requestModel response:responseModel responseCache:cachecallback success:success netError:fail];
}






- (NSURLSessionTask *)afn_DataIsGet:(NSString *)reqMethod
                         andWithURL:(NSString *)url
                             params:(id)params
                        md5Paraname:(nullable NSString *)md5name
                           request:(nullable BaseNetRequestModel *)requestModel
                            response:(nullable BaseNetResponseModel *)responseModel
                      responseCache:(ResponseCache )cachecallback
                            success:(ResponseSuccess )success
                           netError:(ResponseFail )fail {
    
    //读取缓存
    cachecallback!=nil ? cachecallback([PPNetworkCache httpCacheForURL:url parameters:params]) : nil;
    
    if (url == nil) return nil;
    //拼接url
    url = [self baseUrlAppend:url];

    AFHTTPSessionManager* manager = self.manager;
    //签名
    params = [self signPackageWithParam:params andMd5Name:md5name];
    
    NSLog(@"\n\n==========================================================\n请求地址：%@ \n请求参数 %@\n==========================================================\n\n",url,params);
//    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
//    [manager.requestSerializer setValue:@"application/json;charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    requestModel.loadEvent = LC_LOADING;
    
    NSURLSessionTask *sessionTask = nil;
    
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    
    
    if ([reqMethod isEqualToString: @"Get"]) {
        
        sessionTask = [manager GET:url parameters:params progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

            NSLog(@"%@",responseObject);
            
            BaseNetResponseModel *native= [BaseNetResponseModel yy_modelWithDictionary:responseObject];
            responseModel.code = native.code;
            responseModel.msg = native.msg;
            
            if ([native.msg isEqualToString:@"success"]) {
                requestModel.loadEvent = LC_LOAD_SUCCESS;
                //对数据进行异步缓存
                cachecallback!=nil ? [PPNetworkCache setHttpCache:responseObject URL:url parameters:params] : nil;
                
                if (success) success(responseObject);
                
            }else{
                requestModel.loadEvent = LC_LOAD_FAIL;
        
                NSError *otherError = [NSError errorWithDomain:@"otherError" code:-111 userInfo:@{NSLocalizedDescriptionKey:TO_STR(native.msg)}];
                if (fail) fail(otherError);
            }
            
            [[ZY_AfnNetManager tasks]removeObject:task];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            
            responseModel.code = ((NSHTTPURLResponse *)task.response)?((NSHTTPURLResponse *)task.response).statusCode:error.code;
            responseModel.msg = error.localizedDescription;
            
            requestModel.loadInfo  = error.localizedDescription;
            requestModel.loadEvent = LC_LOAD_NET_FAIL;
            
            if (fail) fail(error);
            
            [[ZY_AfnNetManager tasks]removeObject:task];
        }];
    }else{
        sessionTask = [manager POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            
            NSLog(@"%@",responseObject);
            BaseNetResponseModel *native = [BaseNetResponseModel yy_modelWithDictionary:responseObject];
            responseModel.msg = native.msg;
            
            if ([native.msg isEqualToString:@"success"]) {
                requestModel.loadEvent = LC_LOAD_SUCCESS;
                
                if (success) success(responseObject);
            }else{
                requestModel.loadEvent = LC_LOAD_NET_FAIL;
                
                NSError *otherError = [NSError errorWithDomain:@"otherError" code:native.code userInfo:@{NSLocalizedDescriptionKey:TO_STR(native.msg)}];
                if (fail) fail(otherError);
            }
            
            [[ZY_AfnNetManager tasks]removeObject:task];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            
            NSLog(@"%@",error);
            
            [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
            
            responseModel.code = ((NSHTTPURLResponse *)task.response)?((NSHTTPURLResponse *)task.response).statusCode:error.code;
            responseModel.msg = error.localizedDescription;
            
            requestModel.loadInfo  = error.localizedDescription;
            requestModel.loadEvent = LC_LOAD_NET_FAIL;
            
            if (fail) fail(error);
            
            [[ZY_AfnNetManager tasks]removeObject:task];
        }];
    }
    
    sessionTask ? [[ZY_AfnNetManager tasks] addObject:sessionTask]:nil;
    
    
    return sessionTask;
}






/**
 上传图片方法
 
 @param image      上传的图片
 @param url        请求连接，根路径
 @param fileName   图片的名称(如果不传则以当时间命名)
 @param name       上传图片时填写的图片对应的参数
 @param params     参数
 @param progress   上传进度
 @param success    请求成功返回数据
 @param fail       请求失败
 
 @return 上传操作的task
 */
- (NSURLSessionTask *)uploadWithImage:(UIImage *)image
                                  url:(NSString *)url
                             fileName:(NSString *)fileName
                                 name:(NSString *)name
                               params:(NSDictionary *)params
                             progress:(UploadProgress)progress
                              success:(ResponseSuccess)success
                                 fail:(ResponseFail)fail
{
//    if (url == nil) return nil;
//
//    //拼接url
////    url = [self baseUrlAppend:url];
//
//    AFHTTPSessionManager *manager=self.manager;
//
////    params = [self signPackageWithParam:params];
//
//    NSURLSessionTask *sessionTask = [manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
//
//        UIImage *yasuoImg = [UIImage imageCompressToJPGWithImgage:image maxWidth:1000 maxHeight:1000];
//        NSData *imageData = UIImageJPEGRepresentation(yasuoImg, 0.5);
//
//
//        NSString *imageFileName = fileName;
//        if (fileName == nil || ![fileName isKindOfClass:[NSString class]] || fileName.length == 0) {
//            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//            formatter.dateFormat = @"yyyyMMddHHmmss";
//            NSString *str = [formatter stringFromDate:[NSDate date]];
//            imageFileName = [NSString stringWithFormat:@"%@.jpg", str];
//        }
//        NSLog(@"%@",imageFileName);
//        // 上传图片，以文件流的格式
//        [formData appendPartWithFileData:imageData name:name fileName:imageFileName mimeType:@"image/jpeg"];
//    } progress:^(NSProgress * _Nonnull uploadProgress) {
//        NSLog(@"上传进度--%lld,总进度---%lld",uploadProgress.completedUnitCount,uploadProgress.totalUnitCount);
//        if (progress) {
//            progress(uploadProgress.completedUnitCount, uploadProgress.totalUnitCount);
//        }
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
//        NSLog(@"%@",responseObject);
//        BaseNetResponseModel *responseModel = [BaseNetResponseModel yy_modelWithDictionary:responseObject];
//        if (responseModel.code == 200) {
//            if (success) success(responseObject);
//        }else{
//            NSError *otherError = [NSError errorWithDomain:@"otherError" code:responseModel.code userInfo:@{ NSLocalizedDescriptionKey:responseModel.msg}];
//            if (fail) fail(otherError);
//        }
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"%@",error);
//        if (fail) fail(error);
//    }];
////    [sessionTask resume];
//
//    return sessionTask;
    return nil;
}




- (NSURLSessionTask *)getDataFromUrl:(NSString *)url
                              params:(id)params
                             success:(ResponseSuccess)success
                                fail:(ResponseFail )fail{
    
    AFHTTPSessionManager* manager = self.manager;
    AFHTTPRequestSerializer *requestSerializer =  manager.requestSerializer;
    [requestSerializer setValue:@"" forHTTPHeaderField:@"Authorization"];
    
    NSURLSessionTask *sessionTask = nil;
    sessionTask = [manager GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if(success)success(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if(fail)fail(error);
    }];
    return sessionTask;
}




//设备唯一码
//- (NSString *)getUniqueDeviceIdentifierAsString
//{
//    NSString *appName=[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleNameKey];
//    
//    NSString *strApplicationUUID =  [SAMKeychain passwordForService:appName account:@"app_unique_coding"];
//    if (strApplicationUUID == nil)
//    {
//        strApplicationUUID  = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
//        
//        NSError *error = nil;
//        SAMKeychainQuery *query = [[SAMKeychainQuery alloc] init];
//        query.service = appName;
//        query.account = @"app_unique_coding";
//        query.password = strApplicationUUID;
//        query.synchronizationMode = SAMKeychainQuerySynchronizationModeNo;
//        [query save:&error];
//        
//    }
//    if (strApplicationUUID == nil) {
//        return @"";
//    }
//    return strApplicationUUID;
//}

//接口md5Paraname
- (NSString *)KeyReturnMd5ParanameWithUrl:(NSString *)url{
    if ([url isEqualToString:Car_API_CreateIdentifyCode]) {
        return @"TOKEN";
    }else if ([url isEqualToString:Car_API_getSaleCarInfo] || [url isEqualToString:Car_API_getSaleCarCommentList]) {
        return @"SALECAR_ID";
    }else if ([url isEqualToString:Car_API_Register]) {
        return @"USERNAME";
    }
    return @"NULL";
}







- (NSString*)iphoneType {

    struct utsname systemInfo;
    
    uname(&systemInfo);
    
    NSString*platform = [NSString stringWithCString: systemInfo.machine encoding:NSASCIIStringEncoding];
    
    if([platform isEqualToString:@"iPhone1,1"])  return@"iPhone 2G";
    
    if([platform isEqualToString:@"iPhone1,2"])  return@"iPhone 3G";
    
    if([platform isEqualToString:@"iPhone2,1"])  return@"iPhone 3GS";
    
    if([platform isEqualToString:@"iPhone3,1"])  return@"iPhone 4";
    
    if([platform isEqualToString:@"iPhone3,2"])  return@"iPhone 4";
    
    if([platform isEqualToString:@"iPhone3,3"])  return@"iPhone 4";
    
    if([platform isEqualToString:@"iPhone4,1"])  return@"iPhone 4S";
    
    if([platform isEqualToString:@"iPhone5,1"])  return@"iPhone 5";
    
    if([platform isEqualToString:@"iPhone5,2"])  return@"iPhone 5";
    
    if([platform isEqualToString:@"iPhone5,3"])  return@"iPhone 5c";
    
    if([platform isEqualToString:@"iPhone5,4"])  return@"iPhone 5c";
    
    if([platform isEqualToString:@"iPhone6,1"])  return@"iPhone 5s";
    
    if([platform isEqualToString:@"iPhone6,2"])  return@"iPhone 5s";
    
    if([platform isEqualToString:@"iPhone7,1"])  return@"iPhone 6 Plus";
    
    if([platform isEqualToString:@"iPhone7,2"])  return@"iPhone 6";
    
    if([platform isEqualToString:@"iPhone8,1"])  return@"iPhone 6s";
    
    if([platform isEqualToString:@"iPhone8,2"])  return@"iPhone 6s Plus";
    
    if([platform isEqualToString:@"iPhone8,4"])  return@"iPhone SE";
    
    if([platform isEqualToString:@"iPhone9,1"])  return@"iPhone 7";
    
    if([platform isEqualToString:@"iPhone9,2"])  return@"iPhone 7 Plus";
    
    if([platform isEqualToString:@"iPhone10,1"]) return@"iPhone 8";
    
    if([platform isEqualToString:@"iPhone10,4"]) return@"iPhone 8";
    
    if([platform isEqualToString:@"iPhone10,2"]) return@"iPhone 8 Plus";
    
    if([platform isEqualToString:@"iPhone10,5"]) return@"iPhone 8 Plus";
    
    if([platform isEqualToString:@"iPhone10,3"]) return@"iPhone X";
    
    if([platform isEqualToString:@"iPhone10,6"]) return@"iPhone X";
    
    if([platform isEqualToString:@"iPod1,1"])  return@"iPod Touch 1G";
    
    if([platform isEqualToString:@"iPod2,1"])  return@"iPod Touch 2G";
    
    if([platform isEqualToString:@"iPod3,1"])  return@"iPod Touch 3G";
    
    if([platform isEqualToString:@"iPod4,1"])  return@"iPod Touch 4G";
    
    if([platform isEqualToString:@"iPod5,1"])  return@"iPod Touch 5G";
    
    if([platform isEqualToString:@"iPad1,1"])  return@"iPad 1G";
    
    if([platform isEqualToString:@"iPad2,1"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,2"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,3"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,4"])  return@"iPad 2";
    
    if([platform isEqualToString:@"iPad2,5"])  return@"iPad Mini 1G";
    
    if([platform isEqualToString:@"iPad2,6"])  return@"iPad Mini 1G";
    
    if([platform isEqualToString:@"iPad2,7"])  return@"iPad Mini 1G";
    
    if([platform isEqualToString:@"iPad3,1"])  return@"iPad 3";
    
    if([platform isEqualToString:@"iPad3,2"])  return@"iPad 3";
    
    if([platform isEqualToString:@"iPad3,3"])  return@"iPad 3";
    
    if([platform isEqualToString:@"iPad3,4"])  return@"iPad 4";
    
    if([platform isEqualToString:@"iPad3,5"])  return@"iPad 4";
    
    if([platform isEqualToString:@"iPad3,6"])  return@"iPad 4";
    
    if([platform isEqualToString:@"iPad4,1"])  return@"iPad Air";
    
    if([platform isEqualToString:@"iPad4,2"])  return@"iPad Air";
    
    if([platform isEqualToString:@"iPad4,3"])  return@"iPad Air";
    
    if([platform isEqualToString:@"iPad4,4"])  return@"iPad Mini 2G";
    
    if([platform isEqualToString:@"iPad4,5"])  return@"iPad Mini 2G";
    
    if([platform isEqualToString:@"iPad4,6"])  return@"iPad Mini 2G";
    
    if([platform isEqualToString:@"iPad4,7"])  return@"iPad Mini 3";
    
    if([platform isEqualToString:@"iPad4,8"])  return@"iPad Mini 3";
    
    if([platform isEqualToString:@"iPad4,9"])  return@"iPad Mini 3";
    
    if([platform isEqualToString:@"iPad5,1"])  return@"iPad Mini 4";
    
    if([platform isEqualToString:@"iPad5,2"])  return@"iPad Mini 4";
    
    if([platform isEqualToString:@"iPad5,3"])  return@"iPad Air 2";
    
    if([platform isEqualToString:@"iPad5,4"])  return@"iPad Air 2";
    
    if([platform isEqualToString:@"iPad6,3"])  return@"iPad Pro 9.7";
    
    if([platform isEqualToString:@"iPad6,4"])  return@"iPad Pro 9.7";
    
    if([platform isEqualToString:@"iPad6,7"])  return@"iPad Pro 12.9";
    
    if([platform isEqualToString:@"iPad6,8"])  return@"iPad Pro 12.9";
    
    if([platform isEqualToString:@"i386"])  return@"iPhone Simulator";
    
    if([platform isEqualToString:@"x86_64"])  return@"iPhone Simulator";
    
    return platform;
    
}





@end
